package com.example.demo.controller;

import com.example.demo.pojo.ElectricityRate;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.User;
import com.example.demo.pojo.WaterRate;
import com.example.demo.service.RateService;
import com.example.demo.service.UserService;
import com.example.demo.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rate")
public class RateController {

    @Autowired
    private RateService rateService;

    @Autowired
    private UserService userService;

    public boolean Authentication() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String phonenumber = (String) map.get("phonenumber");
        User u = userService.findByphoneNumber(phonenumber);
        if (u.getUsertype() != 1) {
            return false;
        }
        return true;
    }

    @PostMapping("/updatewater")
    public Result updatewater(@RequestBody WaterRate waterRate) {
        if(!Authentication())
            return Result.error("权限不足");
        if(waterRate.getWaterrateid() == null)
            return Result.error("id不能为空");
        rateService.updatewater(waterRate);
        return Result.success();
    }

    @PostMapping("/updateelectricity")
    public Result updateelectricity(@RequestBody ElectricityRate electricityRate) {
        if(!Authentication())
            return Result.error("权限不足");
        if(electricityRate.getElectricityrateid() == null)
            return Result.error("id不能为空");
        rateService.updateelectricity(electricityRate);
        return Result.success();
    }

    @GetMapping("/getwater")
    public Result<List<WaterRate>> getwater() {
        List<WaterRate> waterRate = rateService.getwater();
        return Result.success(waterRate);
    }

    @GetMapping("/getelectricity")
    public Result<List<ElectricityRate>> getelectricity() {
        List<ElectricityRate> electricityRate = rateService.getelectricity();
        return Result.success(electricityRate);
    }



}
